<?php

namespace App\Lib\AliyunSdk;

require_once  EASYSWOOLE_ROOT . '/App/Lib/AliyunSdk/aliyun-php-sdk-core/Config.php';
require_once  EASYSWOOLE_ROOT . '/App/Lib/AliyunSdk/aliyun-php-sdk-oss/autoload.php';

use vod\Request\V20170321 as vod;
use OSS\OssClient;
use OSS\Core\OssException;

class AliVod
{

    public $regionId = 'cn-shanghai';
    public $accessKeyId = 'LTAI4FuXQVoTCZ86fXuJmpK2';
    public $accessKeySecret = 'opj5QXoEIhUzz1BO5d7V4rrZZEybCu';
    public $client = '';
    public $ossClient = '';
         
    public function __construct()
    {
        $profile = \DefaultProfile::getProfile($this->regionId, $this->accessKeyId, $this->accessKeySecret);
        $this->client = new \DefaultAcsClient($profile);
    }

    // 获取视频上传地址和凭证
    public function createUploadVideo($title,$videoName) {
        $request = new vod\CreateUploadVideoRequest();
        $request->setTitle($title);        
        $request->setFileName($videoName); 
        $request->setAcceptFormat('JSON');
        $result = $this->client->getAcsResponse($request);
        if(empty($result) || !isset($result->VideoId)){
            throw new \Exception('上传错误');
        }
        return $result;
    }
    
    // 初始化OSS
    public function initOssClient($uploadAuth, $uploadAddress)
    {
        $this->ossClient = new OssClient($uploadAuth['AccessKeyId'], $uploadAuth['AccessKeySecret'], $uploadAddress['Endpoint'], false, $uploadAuth['SecurityToken']);
        $this->ossClient->setTimeout(86400*7);
        $this->ossClient->setConnectTimeout(10);
    }

    // 上传本地文件
    public function uploadLocalFile($uploadAddress,$localFile)
    {
        return $this->ossClient->uploadFile($uploadAddress['Bucket'], $uploadAddress['FileName'], $localFile);
    }

    public function getPlayInfo($videoId = 0)
    {
        if(empty($videoId)){
            return [];
        }
        $request = new vod\GetPlayInfoRequest();
        $request->setVideoId($videoId);
        $request->setAcceptFormat('JSON');
        return $this->client->getAcsResponse($request);
    }

}